package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.mapper.CollectMapper;
import com.situ.mall.mapper.EvaluateMapper;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.ProductSalesVO;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private EvaluateMapper evaluateMapper;


    @Override
    public PageResult list(ProductQuery productQuery) {

        //使用PageHelper开启分页
        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<ProductVO> list = productMapper.list(productQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        productMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        productMapper.deleteAll(ids);
    }

    @Override
    public void add(Product product) {
        productMapper.insertSelective(product);
    }

    @Override
    public ProductVO selectById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void updateIsDeleted(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer countProductByCategoryId(Integer categoryId) {
        return productMapper.countProductByCategoryId(categoryId);
    }

    @Override
    public void onAll(Integer[] ids) {
        productMapper.onAll(ids);
    }

    @Override
    public void offAll(Integer[] ids) {
        productMapper.offAll(ids);
    }

    @Override
    public Integer judge(Integer id) {
        Integer countCollect = collectMapper.countByProductId(id);
        Integer countCart = cartMapper.countByProductId(id);
        Integer countEvaluate = evaluateMapper.countByProductId(id);
        Integer judge = null;
        if (countEvaluate == 0 && countCart == 0 && countCollect == 0) {
            judge = 1;
        } else {
            judge = 0;
        }
        return judge;
    }

    @Override
    public List<ProductSalesVO> productSales() {
        return productMapper.productSales();
    }

    @Override
    public Integer totalCost() {
        return productMapper.totalCost();
    }
}
